<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>云盘首页</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="layui/dist/css/layui.css"  media="all">
    <script type="text/javascript" src="js/jquery-3.3.1.min.js"></script>
    <script type="text/javascript">
        $(function() {
            $("#oldPassword").blur(function () {
                //验证用户该密码是否存在
                var oldPassword = $("#oldPassword").val();
                $.ajax({
                    url: 'checkOldPasswordByPassword',
                    type: "get",
                    data: {oldPassword: oldPassword},
                    dataType: "json",
                    success: function (data, textStatus) {
                        if (data) {
                            $("#msg").text("旧密码正确，请继续输入新密码")
                        } else {
                            $("#msg").text("旧密码错误，请重新输入");
                            $("#oldPassword").focus();
                        }
                    },
                    error: function () {
                        alert("系统错误,请联系管理员，状态码为：", textStatus);
                    }
                })
            });

            $("#passwordBtn").click(function () {
                var newPassword = $("#newPassword").val();
                var newPassword1 = $("#newPassword1").val();
                var oldPassword = $("#oldPassword").val();
                if (newPassword == "" || newPassword1 == "" || oldPassword == "") {
                    $("#msg").text("旧密码，新密码。密码确认均不能为空！");
                    return false;
                } else if (newPassword1 != newPassword) {
                    $("#msg").text("密码确认不一致");
                    return false;
                } else {
                    $.ajax({
                        url: 'checkOldPasswordByPassword',
                        type: "get",
                        data: {oldPassword: oldPassword},
                        dataType: "json",
                        success: function (data, textStatus) {
                            if (data) {
                                $.ajax({
                                    url: 'modifyPassword',
                                    type: "get",
                                    data: {newPassword: newPassword},
                                    dataType: "json",
                                    success: function (data, textStatus) {
                                        if (data) {
                                            alert("密码修改成功");
                                            $("#msg").text("密码修改成功");
                                            window.location.reload();
                                        } else {
                                            alert("密码修改失败");
                                            $("#msg").text("密码修改失败");
                                        }
                                    },
                                    error: function () {
                                        alert("系统错误,请联系管理员，状态码为：", textStatus);
                                    }
                                })
                            } else {
                                $("#msg").text("旧密码错误，请重新输入");
                                $("#oldPassword").focus();
                            }
                        },
                        error: function () {
                            alert("系统错误,请联系管理员，状态码为：", textStatus);
                        }
                    })
                }
            });


        });

    </script>
</head>
<body>

<div style="margin: 0 auto; width:60%">
    <form class="layui-form" action="saveUser" lay-filter="example">
        <h1 style="color: #1E9FFF;text-align: center;margin-top: 100px;margin-bottom: 50px">密码修改</h1>
        <div class="layui-form-item">
            <label class="layui-form-label">旧密码：</label>
            <div class="layui-input-block">
                <input type="password" name="oldPassword" id="oldPassword" placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">新密码：</label>
            <div class="layui-input-block">
                <input type="password" name="newPassword" id="newPassword" placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">新密码确认：</label>
            <div class="layui-input-block">
                <input type="password" name="newPassword1" id="newPassword1" placeholder="请输入密码" autocomplete="off" class="layui-input">
            </div>
        </div>

        <p style="height: 50px; text-align: center"> <span id="msg"></span></p>
        <div class="layui-form-item" style="text-align: center">
            <div class="">
                <button type="button" class="layui-btn" lay-submit=""  id="passwordBtn" lay-filter="demo1">修改密码</button>
            </div>
        </div>
    </form>
</div>
</body>
</html>